package cn.tedu.syn;
/**本类用于复习多线程编辑实现方案1:extends Thread*/
public class TestThread1 {
    public static void main(String[] args) {
        TicketThread t1 = new TicketThread();
        TicketThread t2 = new TicketThread();
        TicketThread t3 = new TicketThread();
        TicketThread t4 = new TicketThread();
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}

class TicketThread extends Thread{
    static int tickets = 100;
//    Object o = new Object();//这样不行需要加静态
//    static Object o = new Object();//可以实现，但是不推荐，太麻烦，注意加静态
    @Override
    public void run() {
        while (true){
           synchronized (TicketThread.class){
              if (tickets >0){
                  try {
                      Thread.sleep(10);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  System.out.println(getName()+"="+tickets--);
              }
               if (tickets<=0)break;
           }
        }
    }
}